#include "calendar.h"
#include "smart_home_ui.h"
#include "weather.h"
#include "cJSON.h"
#include "myfont.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sys/wait.h>

// 全局变量
lv_obj_t *calendar_screen = NULL;
static bool calendar_switching = false;  // 防止重复切换的标志

// 老黄历显示对象
static lv_obj_t *top_date_overview = NULL;    // 顶部日期概览
static lv_obj_t *calendar_date_label = NULL;
static lv_obj_t *calendar_lunar_label = NULL;
static lv_obj_t *calendar_ganzhi_label = NULL;
static lv_obj_t *calendar_zodiac_label = NULL;
static lv_obj_t *calendar_xiu_label = NULL;
static lv_obj_t *calendar_yi_label = NULL;
static lv_obj_t *calendar_ji_label = NULL;
static lv_obj_t *calendar_taishen_label = NULL;
static lv_obj_t *calendar_chong_label = NULL;
static lv_obj_t *calendar_pengzu_label = NULL;
static lv_obj_t *calendar_wuxing_label = NULL;

// 替换字符串中的中文逗号为英文逗号
static void replace_chinese_comma(char *str) {
    if (!str) return;
    
    char *pos = str;
    while ((pos = strstr(pos, "，")) != NULL) {
        // 中文逗号是3字节UTF-8字符，替换为1字节英文逗号
        *pos = ',';  // 替换第一个字节
        // 移除后面两个字节
        memmove(pos + 1, pos + 3, strlen(pos + 3) + 1);
        pos++;  // 移动到下一个位置继续查找
    }
}

// 使用系统命令获取HTTP数据
static char* fetch_http_data(const char *url) {
    char *response_data = NULL;
    FILE *fp;
    
    // 在堆上分配命令缓冲区
    char *command = malloc(512);
    if (!command) {
        printf("内存分配失败\n");
        return NULL;
    }
    
    // 构建wget命令
    snprintf(command, 512, "wget -q -O - '%s' 2>/dev/null", url);
    
    fp = popen(command, "r");
    free(command);  // 立即释放命令缓冲区
    
    if (!fp) {
        printf("无法执行wget命令\n");
        return NULL;
    }
    
    // 在堆上分配读取缓冲区
    char *buffer = malloc(4096);
    if (!buffer) {
        printf("内存分配失败\n");
        pclose(fp);
        return NULL;
    }
    
    size_t total_size = 0;
    size_t chunk_size;
    
    while ((chunk_size = fread(buffer, 1, 4096, fp)) > 0) {
        char *new_data = realloc(response_data, total_size + chunk_size + 1);
        if (!new_data) {
            printf("内存分配失败\n");
            free(response_data);
            free(buffer);
            pclose(fp);
            return NULL;
        }
        response_data = new_data;
        memcpy(response_data + total_size, buffer, chunk_size);
        total_size += chunk_size;
    }
    
    free(buffer);  // 释放读取缓冲区
    pclose(fp);
    
    if (response_data) {
        response_data[total_size] = '\0';
    }
    
    return response_data;
}

// 获取老黄历数据
bool fetch_calendar_data(calendar_data_t *data) {
    if (!data) return false;
    
    // 初始化数据
    memset(data, 0, sizeof(calendar_data_t));
    
    const char *api_url = "http://cn.apihz.cn/api/time/getday.php?id=10007135&key=25650215fd8d7c4eb33aece305da110c";
    
    char *response_data = fetch_http_data(api_url);
    if (!response_data) {
        printf("老黄历API请求失败\n");
        return false;
    }
    
    // 解析JSON数据
    cJSON *json = cJSON_Parse(response_data);
    if (!json) {
        printf("老黄历JSON解析失败\n");
        free(response_data);
        return false;
    }
    
    // 提取数据
    cJSON *item;
    
    // 基本日期信息
    if ((item = cJSON_GetObjectItem(json, "id"))) {
        strncpy(data->id, item->valuestring, sizeof(data->id) - 1);
    }
    if ((item = cJSON_GetObjectItem(json, "ynian"))) {
        strncpy(data->ynian, item->valuestring, sizeof(data->ynian) - 1);
    }
    if ((item = cJSON_GetObjectItem(json, "yyue"))) {
        strncpy(data->yyue, item->valuestring, sizeof(data->yyue) - 1);
    }
    if ((item = cJSON_GetObjectItem(json, "yri"))) {
        strncpy(data->yri, item->valuestring, sizeof(data->yri) - 1);
    }
    if ((item = cJSON_GetObjectItem(json, "xingqi"))) {
        strncpy(data->xingqi, item->valuestring, sizeof(data->xingqi) - 1);
    }
    if ((item = cJSON_GetObjectItem(json, "xingzuo"))) {
        strncpy(data->xingzuo, item->valuestring, sizeof(data->xingzuo) - 1);
    }
    
    // 阴历信息
    if ((item = cJSON_GetObjectItem(json, "nnian"))) {
        strncpy(data->nnian, item->valuestring, sizeof(data->nnian) - 1);
    }
    if ((item = cJSON_GetObjectItem(json, "nyue"))) {
        strncpy(data->nyue, item->valuestring, sizeof(data->nyue) - 1);
    }
    if ((item = cJSON_GetObjectItem(json, "nri"))) {
        strncpy(data->nri, item->valuestring, sizeof(data->nri) - 1);
    }
    
    // 干支信息
    if ((item = cJSON_GetObjectItem(json, "ganzhinian"))) {
        strncpy(data->ganzhinian, item->valuestring, sizeof(data->ganzhinian) - 1);
    }
    if ((item = cJSON_GetObjectItem(json, "ganzhiyue"))) {
        strncpy(data->ganzhiyue, item->valuestring, sizeof(data->ganzhiyue) - 1);
    }
    if ((item = cJSON_GetObjectItem(json, "ganzhiri"))) {
        strncpy(data->ganzhiri, item->valuestring, sizeof(data->ganzhiri) - 1);
    }
    
    // 节气物候
    if ((item = cJSON_GetObjectItem(json, "jieqi"))) {
        strncpy(data->jieqi, item->valuestring, sizeof(data->jieqi) - 1);
    }
    if ((item = cJSON_GetObjectItem(json, "jieqimsg"))) {
        strncpy(data->jieqimsg, item->valuestring, sizeof(data->jieqimsg) - 1);
    }
    if ((item = cJSON_GetObjectItem(json, "WUHOU"))) {
        strncpy(data->wuhou, item->valuestring, sizeof(data->wuhou) - 1);
    }
    if ((item = cJSON_GetObjectItem(json, "SHUJIU"))) {
        strncpy(data->shujiu, item->valuestring, sizeof(data->shujiu) - 1);
    }
    if ((item = cJSON_GetObjectItem(json, "FU"))) {
        strncpy(data->fu, item->valuestring, sizeof(data->fu) - 1);
    }
    
    // 星宿信息
    if ((item = cJSON_GetObjectItem(json, "XIU"))) {
        strncpy(data->xiu, item->valuestring, sizeof(data->xiu) - 1);
    }
    if ((item = cJSON_GetObjectItem(json, "XIUANIMAL"))) {
        strncpy(data->xiuanimal, item->valuestring, sizeof(data->xiuanimal) - 1);
    }
    if ((item = cJSON_GetObjectItem(json, "XIULUCK"))) {
        strncpy(data->xiuluck, item->valuestring, sizeof(data->xiuluck) - 1);
    }
    if ((item = cJSON_GetObjectItem(json, "XIUSONG"))) {
        strncpy(data->xiusong, item->valuestring, sizeof(data->xiusong) - 1);
    }
    
    // 方位信息
    if ((item = cJSON_GetObjectItem(json, "taishen"))) {
        strncpy(data->taishen, item->valuestring, sizeof(data->taishen) - 1);
    }
    if ((item = cJSON_GetObjectItem(json, "DAYPOSITIONXI"))) {
        strncpy(data->daypositionxi, item->valuestring, sizeof(data->daypositionxi) - 1);
    }
    if ((item = cJSON_GetObjectItem(json, "xiangchong"))) {
        strncpy(data->xiangchong, item->valuestring, sizeof(data->xiangchong) - 1);
    }
    
    // 宜忌
    if ((item = cJSON_GetObjectItem(json, "yi"))) {
        strncpy(data->yi, item->valuestring, sizeof(data->yi) - 1);
    }
    if ((item = cJSON_GetObjectItem(json, "ji"))) {
        strncpy(data->ji, item->valuestring, sizeof(data->ji) - 1);
    }
    if ((item = cJSON_GetObjectItem(json, "DAYJISHEN"))) {
        strncpy(data->dayjishen, item->valuestring, sizeof(data->dayjishen) - 1);
    }
    if ((item = cJSON_GetObjectItem(json, "DAYXIONGSHA"))) {
        strncpy(data->dayxiongsha, item->valuestring, sizeof(data->dayxiongsha) - 1);
    }
    
    // 彭祖百忌
    if ((item = cJSON_GetObjectItem(json, "pengzu"))) {
        strncpy(data->pengzu, item->valuestring, sizeof(data->pengzu) - 1);
    }
    
    // 其他信息
    if ((item = cJSON_GetObjectItem(json, "DAYTIANSHEN"))) {
        strncpy(data->daytianshen, item->valuestring, sizeof(data->daytianshen) - 1);
    }
    if ((item = cJSON_GetObjectItem(json, "DAYTIANSHENLUCK"))) {
        strncpy(data->daytianshenluck, item->valuestring, sizeof(data->daytianshenluck) - 1);
    }
    if ((item = cJSON_GetObjectItem(json, "ZHIXING"))) {
        strncpy(data->zhixing, item->valuestring, sizeof(data->zhixing) - 1);
    }
    if ((item = cJSON_GetObjectItem(json, "YUEXIANG"))) {
        strncpy(data->yuexiang, item->valuestring, sizeof(data->yuexiang) - 1);
    }
    if ((item = cJSON_GetObjectItem(json, "jijie"))) {
        strncpy(data->jijie, item->valuestring, sizeof(data->jijie) - 1);
    }
    if ((item = cJSON_GetObjectItem(json, "nianwuxing"))) {
        strncpy(data->nianwuxing, item->valuestring, sizeof(data->nianwuxing) - 1);
    }
    if ((item = cJSON_GetObjectItem(json, "yuewuxing"))) {
        strncpy(data->yuewuxing, item->valuestring, sizeof(data->yuewuxing) - 1);
    }
    if ((item = cJSON_GetObjectItem(json, "riwuxing"))) {
        strncpy(data->riwuxing, item->valuestring, sizeof(data->riwuxing) - 1);
    }
    
    data->is_valid = true;
    
    cJSON_Delete(json);
    free(response_data);
    
    printf("老黄历数据获取成功\n");
    return true;
}

// 更新老黄历显示
void update_calendar_display(const calendar_data_t *data) {
    if (!data || !data->is_valid) return;
    
    char text_buffer[512];
    
    // 更新顶部日期概览
    if (top_date_overview && lv_obj_is_valid(top_date_overview)) {
        char nyue_processed[32];
        strncpy(nyue_processed, data->nyue, sizeof(nyue_processed) - 1);
        nyue_processed[sizeof(nyue_processed) - 1] = '\0';
        replace_chinese_comma(nyue_processed);
        snprintf(text_buffer, sizeof(text_buffer), "今日: %s月%s日 %s", 
                data->yyue, data->yri, nyue_processed);
        lv_label_set_text(top_date_overview, text_buffer);
    }
    
    // 更新日期信息
    if (calendar_date_label && lv_obj_is_valid(calendar_date_label)) {
        snprintf(text_buffer, sizeof(text_buffer), "%s年%s月%s日 星期%s", 
                data->ynian, data->yyue, data->yri, data->xingqi);
        lv_label_set_text(calendar_date_label, text_buffer);
    }
    
    // 更新阴历信息
    if (calendar_lunar_label && lv_obj_is_valid(calendar_lunar_label)) {
        char nnian_processed[32], nyue_processed[32], nri_processed[16];
        
        strncpy(nnian_processed, data->nnian, sizeof(nnian_processed) - 1);
        nnian_processed[sizeof(nnian_processed) - 1] = '\0';
        replace_chinese_comma(nnian_processed);
        
        strncpy(nyue_processed, data->nyue, sizeof(nyue_processed) - 1);
        nyue_processed[sizeof(nyue_processed) - 1] = '\0';
        replace_chinese_comma(nyue_processed);
        
        strncpy(nri_processed, data->nri, sizeof(nri_processed) - 1);
        nri_processed[sizeof(nri_processed) - 1] = '\0';
        replace_chinese_comma(nri_processed);
        
        snprintf(text_buffer, sizeof(text_buffer), "农历: %s %s%s", 
                nnian_processed, nyue_processed, nri_processed);
        lv_label_set_text(calendar_lunar_label, text_buffer);
    }
    
    // 更新干支信息
    if (calendar_ganzhi_label && lv_obj_is_valid(calendar_ganzhi_label)) {
        snprintf(text_buffer, sizeof(text_buffer), "干支: %s年 %s月 %s日", 
                data->ganzhinian, data->ganzhiyue, data->ganzhiri);
        lv_label_set_text(calendar_ganzhi_label, text_buffer);
    }
    
    // 更新星座和节气
    if (calendar_zodiac_label && lv_obj_is_valid(calendar_zodiac_label)) {
        snprintf(text_buffer, sizeof(text_buffer), "星座: %s  节气: %s", 
                data->xingzuo, data->jieqi);
        lv_label_set_text(calendar_zodiac_label, text_buffer);
    }
    
    // 节气描述已移除，不再更新
    
    // 更新星宿信息
    if (calendar_xiu_label && lv_obj_is_valid(calendar_xiu_label)) {
        snprintf(text_buffer, sizeof(text_buffer), "星宿: %s(%s) %s", 
                data->xiu, data->xiuanimal, data->xiuluck);
        lv_label_set_text(calendar_xiu_label, text_buffer);
    }
    
    // 星宿歌诀已移除，不再更新
    
    // 更新宜
    if (calendar_yi_label && lv_obj_is_valid(calendar_yi_label) && strlen(data->yi) > 0) {
        char yi_processed[256];
        strncpy(yi_processed, data->yi, sizeof(yi_processed) - 1);
        yi_processed[sizeof(yi_processed) - 1] = '\0';
        replace_chinese_comma(yi_processed);
        snprintf(text_buffer, sizeof(text_buffer), "宜: %s", yi_processed);
        lv_label_set_text(calendar_yi_label, text_buffer);
    }
    
    // 更新忌
    if (calendar_ji_label && lv_obj_is_valid(calendar_ji_label) && strlen(data->ji) > 0) {
        char ji_processed[256];
        strncpy(ji_processed, data->ji, sizeof(ji_processed) - 1);
        ji_processed[sizeof(ji_processed) - 1] = '\0';
        replace_chinese_comma(ji_processed);
        snprintf(text_buffer, sizeof(text_buffer), "忌: %s", ji_processed);
        lv_label_set_text(calendar_ji_label, text_buffer);
    }
    
    // 更新胎神方位
    if (calendar_taishen_label && lv_obj_is_valid(calendar_taishen_label) && strlen(data->taishen) > 0) {
        snprintf(text_buffer, sizeof(text_buffer), "胎神: %s", data->taishen);
        lv_label_set_text(calendar_taishen_label, text_buffer);
    }
    
    // 更新冲煞
    if (calendar_chong_label && lv_obj_is_valid(calendar_chong_label) && strlen(data->xiangchong) > 0) {
        snprintf(text_buffer, sizeof(text_buffer), "冲煞: %s", data->xiangchong);
        lv_label_set_text(calendar_chong_label, data->xiangchong);
    }
    
    // 更新彭祖百忌
    if (calendar_pengzu_label && lv_obj_is_valid(calendar_pengzu_label) && strlen(data->pengzu) > 0) {
        char pengzu_processed[256];
        strncpy(pengzu_processed, data->pengzu, sizeof(pengzu_processed) - 1);
        pengzu_processed[sizeof(pengzu_processed) - 1] = '\0';
        replace_chinese_comma(pengzu_processed);
        snprintf(text_buffer, sizeof(text_buffer), "彭祖百忌: %s", pengzu_processed);
        lv_label_set_text(calendar_pengzu_label, text_buffer);
    }
    
    // 更新五行
    if (calendar_wuxing_label && lv_obj_is_valid(calendar_wuxing_label)) {
        char nianwuxing_processed[32], yuewuxing_processed[32], riwuxing_processed[32];
        
        strncpy(nianwuxing_processed, data->nianwuxing, sizeof(nianwuxing_processed) - 1);
        nianwuxing_processed[sizeof(nianwuxing_processed) - 1] = '\0';
        replace_chinese_comma(nianwuxing_processed);
        
        strncpy(yuewuxing_processed, data->yuewuxing, sizeof(yuewuxing_processed) - 1);
        yuewuxing_processed[sizeof(yuewuxing_processed) - 1] = '\0';
        replace_chinese_comma(yuewuxing_processed);
        
        strncpy(riwuxing_processed, data->riwuxing, sizeof(riwuxing_processed) - 1);
        riwuxing_processed[sizeof(riwuxing_processed) - 1] = '\0';
        replace_chinese_comma(riwuxing_processed);
        
        snprintf(text_buffer, sizeof(text_buffer), "五行: 年%s 月%s 日%s", 
                nianwuxing_processed, yuewuxing_processed, riwuxing_processed);
        lv_label_set_text(calendar_wuxing_label, text_buffer);
    }
}

// 延迟切换到天气界面的回调函数
static void delayed_weather_switch_cb(lv_timer_t *timer) {
    // 不调用 cleanup_calendar_ui()，让 LVGL 的 lv_scr_load_anim 自动处理旧屏幕清理
    // 只重置全局指针，避免在事件回调中删除当前屏幕
    top_date_overview = NULL;
    calendar_date_label = NULL;
    calendar_lunar_label = NULL;
    calendar_ganzhi_label = NULL;
    calendar_zodiac_label = NULL;
    calendar_xiu_label = NULL;
    calendar_yi_label = NULL;
    calendar_ji_label = NULL;
    calendar_taishen_label = NULL;
    calendar_chong_label = NULL;
    calendar_pengzu_label = NULL;
    calendar_wuxing_label = NULL;
    calendar_screen = NULL;  // 只重置指针，不删除对象
    
    create_weather_screen();
    calendar_switching = false;  // 重置切换标志
    lv_timer_del(timer);  // 删除定时器
}

// 延迟返回首页的回调函数
static void delayed_home_switch_cb(lv_timer_t *timer) {
    // 不调用 cleanup_calendar_ui()，让 LVGL 的 lv_scr_load_anim 自动处理旧屏幕清理
    // 只重置全局指针，避免在事件回调中删除当前屏幕
    top_date_overview = NULL;
    calendar_date_label = NULL;
    calendar_lunar_label = NULL;
    calendar_ganzhi_label = NULL;
    calendar_zodiac_label = NULL;
    calendar_xiu_label = NULL;
    calendar_yi_label = NULL;
    calendar_ji_label = NULL;
    calendar_taishen_label = NULL;
    calendar_chong_label = NULL;
    calendar_pengzu_label = NULL;
    calendar_wuxing_label = NULL;
    calendar_screen = NULL;  // 只重置指针，不删除对象
    
    return_to_smart_home_ui();
    calendar_switching = false;  // 重置切换标志
    lv_timer_del(timer);  // 删除定时器
}

// 滑动手势处理函数
static void calendar_gesture_event_cb(lv_event_t *e) {
    lv_event_code_t code = lv_event_get_code(e);
    
    if (code == LV_EVENT_GESTURE) {
        // 防止重复切换
        if (calendar_switching) {
            return;
        }
        
        lv_dir_t dir = lv_indev_get_gesture_dir(lv_indev_get_act());
        if (dir == LV_DIR_RIGHT) {
            // 左滑手势，返回天气页面
            printf("检测到左滑手势，返回天气页面\n");
            calendar_switching = true;  // 设置切换标志
            lv_timer_create(delayed_weather_switch_cb, 5, NULL);
        }
    }
}

// 返回按钮回调函数
static void btn_back_event_cb(lv_event_t *e) {
    lv_event_code_t code = lv_event_get_code(e);
    if (code == LV_EVENT_CLICKED) {
        // 防止重复切换
        if (calendar_switching) {
            return;
        }
        
        // 延迟返回首页
        calendar_switching = true;  // 设置切换标志
        lv_timer_create(delayed_home_switch_cb, 5, NULL);
    }
}

// 创建老黄历页面
void create_calendar_screen(void) {
    // 重置切换标志
    calendar_switching = false;
    
    // 如果页面已存在，先清理
    if (calendar_screen) {
        cleanup_calendar_ui();
    }
    

    
    // 创建主屏幕
    calendar_screen = lv_obj_create(NULL);
    lv_obj_set_style_bg_color(calendar_screen, lv_color_hex(0x2C3E50), LV_PART_MAIN);
    
    // 添加滑动手势支持
    lv_obj_add_event_cb(calendar_screen, calendar_gesture_event_cb, LV_EVENT_GESTURE, NULL);
    
    // 返回按钮
    lv_obj_t *btn_back = lv_btn_create(calendar_screen);
    lv_obj_set_size(btn_back, 100, 40);
    lv_obj_align(btn_back, LV_ALIGN_TOP_LEFT, 20, 20);
    lv_obj_set_style_bg_color(btn_back, lv_color_hex(0x34495e), LV_PART_MAIN);
    lv_obj_set_style_border_width(btn_back, 0, LV_PART_MAIN);
    lv_obj_set_style_radius(btn_back, 10, LV_PART_MAIN);
    lv_obj_add_event_cb(btn_back, btn_back_event_cb, LV_EVENT_CLICKED, NULL);
    
    lv_obj_t *back_label = lv_label_create(btn_back);
    lv_label_set_text(back_label, LV_SYMBOL_LEFT " 返回");
    lv_obj_set_style_text_font(back_label, &lv_font_source_han_sans_bold_20, 0);
    lv_obj_set_style_text_color(back_label, lv_color_hex(0xFFFFFF), 0);
    lv_obj_center(back_label);
    
    // 标题
    lv_obj_t *title_label = lv_label_create(calendar_screen);
    lv_label_set_text(title_label, "老黄历");
    lv_obj_set_style_text_font(title_label, &lv_font_source_han_sans_bold_20, 0);
    lv_obj_set_style_text_color(title_label, lv_color_hex(0xFFFFFF), 0);
    lv_obj_align(title_label, LV_ALIGN_TOP_MID, 0, 10);
    
    // 顶部简洁日期概览（防手指遮挡）
    top_date_overview = lv_label_create(calendar_screen);
    lv_label_set_text(top_date_overview, "今日: 加载中...");
    lv_obj_set_style_text_font(top_date_overview, &lv_font_source_han_sans_bold_20, 0);
    lv_obj_set_style_text_color(top_date_overview, lv_color_hex(0xF39C12), 0);
    lv_obj_align(top_date_overview, LV_ALIGN_TOP_MID, 0, 40);
    
    // 创建主容器（固定不滚动）
    lv_obj_t *scroll_container = lv_obj_create(calendar_screen);
    lv_obj_set_size(scroll_container, 760, 710);
    lv_obj_align(scroll_container, LV_ALIGN_TOP_MID, 0, 75);
    lv_obj_set_style_bg_color(scroll_container, lv_color_hex(0x34495e), LV_PART_MAIN);
    lv_obj_set_style_border_width(scroll_container, 0, LV_PART_MAIN);
    lv_obj_set_style_radius(scroll_container, 15, LV_PART_MAIN);
    lv_obj_set_style_pad_all(scroll_container, 15, LV_PART_MAIN);
    lv_obj_clear_flag(scroll_container, LV_OBJ_FLAG_SCROLLABLE);  // 禁用滚动
    
    // ==================== 日期区域 ====================
    lv_obj_t *date_container = lv_obj_create(scroll_container);
    lv_obj_set_size(date_container, 720, 130);
    lv_obj_align(date_container, LV_ALIGN_TOP_MID, 0, 0);
    lv_obj_set_style_bg_color(date_container, lv_color_hex(0x2c3e50), LV_PART_MAIN);
    lv_obj_set_style_border_width(date_container, 2, LV_PART_MAIN);
    lv_obj_set_style_border_color(date_container, lv_color_hex(0xF39C12), LV_PART_MAIN);
    lv_obj_set_style_radius(date_container, 10, LV_PART_MAIN);
    lv_obj_set_style_pad_all(date_container, 25, LV_PART_MAIN);
    lv_obj_clear_flag(date_container, LV_OBJ_FLAG_SCROLLABLE);
    
    // 阳历日期（大字体）
    calendar_date_label = lv_label_create(date_container);
    lv_label_set_text(calendar_date_label, "加载中...");
    lv_obj_set_style_text_font(calendar_date_label, &lv_font_source_han_sans_bold_20, 0);
    lv_obj_set_style_text_color(calendar_date_label, lv_color_hex(0xFFFFFF), 0);
    lv_obj_set_style_text_align(calendar_date_label, LV_TEXT_ALIGN_CENTER, 0);
    lv_obj_set_style_transform_zoom(calendar_date_label, 110, 0);
    lv_obj_align(calendar_date_label, LV_ALIGN_TOP_MID, 0, 10);
    
    // 农历日期
    calendar_lunar_label = lv_label_create(date_container);
    lv_label_set_text(calendar_lunar_label, "农历: 加载中...");
    lv_obj_set_style_text_font(calendar_lunar_label, &lv_font_source_han_sans_bold_20, 0);
    lv_obj_set_style_text_color(calendar_lunar_label, lv_color_hex(0xF39C12), 0);
    lv_obj_set_style_text_align(calendar_lunar_label, LV_TEXT_ALIGN_CENTER, 0);
    lv_obj_align_to(calendar_lunar_label, calendar_date_label, LV_ALIGN_OUT_BOTTOM_MID, 0, 20);
    
    // ==================== 基本信息区域 ====================
    lv_obj_t *basic_container = lv_obj_create(scroll_container);
    lv_obj_set_size(basic_container, 720, 160);
    lv_obj_align_to(basic_container, date_container, LV_ALIGN_OUT_BOTTOM_MID, 0, 12);
    lv_obj_set_style_bg_color(basic_container, lv_color_hex(0x2c3e50), LV_PART_MAIN);
    lv_obj_set_style_border_width(basic_container, 2, LV_PART_MAIN);
    lv_obj_set_style_border_color(basic_container, lv_color_hex(0x3498DB), LV_PART_MAIN);
    lv_obj_set_style_radius(basic_container, 10, LV_PART_MAIN);
    lv_obj_set_style_pad_all(basic_container, 25, LV_PART_MAIN);
    lv_obj_clear_flag(basic_container, LV_OBJ_FLAG_SCROLLABLE);
    
    // 干支信息
    calendar_ganzhi_label = lv_label_create(basic_container);
    lv_label_set_text(calendar_ganzhi_label, "干支: 加载中...");
    lv_obj_set_style_text_font(calendar_ganzhi_label, &lv_font_source_han_sans_bold_20, 0);
    lv_obj_set_style_text_color(calendar_ganzhi_label, lv_color_hex(0xE74C3C), 0);
    lv_obj_align(calendar_ganzhi_label, LV_ALIGN_TOP_LEFT, 0, 0);
    
    // 星座节气信息
    calendar_zodiac_label = lv_label_create(basic_container);
    lv_label_set_text(calendar_zodiac_label, "星座: 加载中...  节气: 加载中...");
    lv_obj_set_style_text_font(calendar_zodiac_label, &lv_font_source_han_sans_bold_20, 0);
    lv_obj_set_style_text_color(calendar_zodiac_label, lv_color_hex(0x9B59B6), 0);
    lv_obj_align_to(calendar_zodiac_label, calendar_ganzhi_label, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 15);
    
    // 星宿信息
    calendar_xiu_label = lv_label_create(basic_container);
    lv_label_set_text(calendar_xiu_label, "星宿: 加载中...");
    lv_obj_set_style_text_font(calendar_xiu_label, &lv_font_source_han_sans_bold_20, 0);
    lv_obj_set_style_text_color(calendar_xiu_label, lv_color_hex(0x3498DB), 0);
    lv_obj_align_to(calendar_xiu_label, calendar_zodiac_label, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 15);
    
    // 五行信息（移到基本信息区域）
    calendar_wuxing_label = lv_label_create(basic_container);
    lv_label_set_text(calendar_wuxing_label, "五行: 加载中...");
    lv_obj_set_style_text_font(calendar_wuxing_label, &lv_font_source_han_sans_bold_20, 0);
    lv_obj_set_style_text_color(calendar_wuxing_label, lv_color_hex(0x1ABC9C), 0);
    lv_obj_align_to(calendar_wuxing_label, calendar_xiu_label, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 15);
    
    // ==================== 中下区域：宜忌 ====================
    lv_obj_t *yiji_container = lv_obj_create(scroll_container);
    lv_obj_set_size(yiji_container, 720, 150);
    lv_obj_align_to(yiji_container, basic_container, LV_ALIGN_OUT_BOTTOM_MID, 0, 12);
    lv_obj_set_style_bg_color(yiji_container, lv_color_hex(0x2c3e50), LV_PART_MAIN);
    lv_obj_set_style_border_width(yiji_container, 2, LV_PART_MAIN);
    lv_obj_set_style_border_color(yiji_container, lv_color_hex(0x27AE60), LV_PART_MAIN);
    lv_obj_set_style_radius(yiji_container, 10, LV_PART_MAIN);
    lv_obj_set_style_pad_all(yiji_container, 25, LV_PART_MAIN);
    lv_obj_clear_flag(yiji_container, LV_OBJ_FLAG_SCROLLABLE);
    
    // 宜
    calendar_yi_label = lv_label_create(yiji_container);
    lv_label_set_text(calendar_yi_label, "宜: 加载中...");
    lv_obj_set_style_text_font(calendar_yi_label, &lv_font_source_han_sans_bold_20, 0);
    lv_obj_set_style_text_color(calendar_yi_label, lv_color_hex(0x27AE60), 0);
    lv_obj_set_width(calendar_yi_label, 680);
    lv_label_set_long_mode(calendar_yi_label, LV_LABEL_LONG_WRAP);
    lv_obj_align(calendar_yi_label, LV_ALIGN_TOP_LEFT, 0, 0);
    
    // 忌
    calendar_ji_label = lv_label_create(yiji_container);
    lv_label_set_text(calendar_ji_label, "忌: 加载中...");
    lv_obj_set_style_text_font(calendar_ji_label, &lv_font_source_han_sans_bold_20, 0);
    lv_obj_set_style_text_color(calendar_ji_label, lv_color_hex(0xE67E22), 0);
    lv_obj_set_width(calendar_ji_label, 680);
    lv_label_set_long_mode(calendar_ji_label, LV_LABEL_LONG_WRAP);
    lv_obj_align_to(calendar_ji_label, calendar_yi_label, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 35);
    
    // ==================== 底部区域：其他信息 ====================
    lv_obj_t *other_container = lv_obj_create(scroll_container);
    lv_obj_set_size(other_container, 720, 150);
    lv_obj_align_to(other_container, yiji_container, LV_ALIGN_OUT_BOTTOM_MID, 0, 12);
    lv_obj_set_style_bg_color(other_container, lv_color_hex(0x2c3e50), LV_PART_MAIN);
    lv_obj_set_style_border_width(other_container, 2, LV_PART_MAIN);
    lv_obj_set_style_border_color(other_container, lv_color_hex(0x95A5A6), LV_PART_MAIN);
    lv_obj_set_style_radius(other_container, 10, LV_PART_MAIN);
    lv_obj_set_style_pad_all(other_container, 25, LV_PART_MAIN);
    lv_obj_clear_flag(other_container, LV_OBJ_FLAG_SCROLLABLE);
    
    // 胎神方位
    calendar_taishen_label = lv_label_create(other_container);
    lv_label_set_text(calendar_taishen_label, "胎神: 加载中...");
    lv_obj_set_style_text_font(calendar_taishen_label, &lv_font_source_han_sans_bold_20, 0);
    lv_obj_set_style_text_color(calendar_taishen_label, lv_color_hex(0xBDC3C7), 0);
    lv_obj_align(calendar_taishen_label, LV_ALIGN_TOP_LEFT, 0, 0);
    
    // 冲煞
    calendar_chong_label = lv_label_create(other_container);
    lv_label_set_text(calendar_chong_label, "冲煞: 加载中...");
    lv_obj_set_style_text_font(calendar_chong_label, &lv_font_source_han_sans_bold_20, 0);
    lv_obj_set_style_text_color(calendar_chong_label, lv_color_hex(0xBDC3C7), 0);
    lv_obj_align_to(calendar_chong_label, calendar_taishen_label, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 15);
    
    // 彭祖百忌
    calendar_pengzu_label = lv_label_create(other_container);
    lv_label_set_text(calendar_pengzu_label, "彭祖百忌: 加载中...");
    lv_obj_set_style_text_font(calendar_pengzu_label, &lv_font_source_han_sans_bold_20, 0);
    lv_obj_set_style_text_color(calendar_pengzu_label, lv_color_hex(0xBDC3C7), 0);
    lv_obj_set_width(calendar_pengzu_label, 680);
    lv_label_set_long_mode(calendar_pengzu_label, LV_LABEL_LONG_WRAP);
    lv_obj_align_to(calendar_pengzu_label, calendar_chong_label, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 15);
    
    // 异步获取数据
    calendar_data_t *data = malloc(sizeof(calendar_data_t));
    if (data && fetch_calendar_data(data)) {
        update_calendar_display(data);
        free(data);
    } else {
        printf("老黄历数据获取失败\n");
        if (data) free(data);
    }
    
    // 使用安全的屏幕切换方式，避免段错误
    // 使用快速淡入动画，提供更流畅的切换体验
    lv_scr_load_anim(calendar_screen, LV_SCR_LOAD_ANIM_FADE_ON, 100, 0, false);
    
    // 让LVGL自动处理旧屏幕清理，避免手动删除导致的内存问题
}

// 清理老黄历UI
void cleanup_calendar_ui(void) {
    // 重置所有标签指针（子对象会随父对象一起删除）
    top_date_overview = NULL;
    calendar_date_label = NULL;
    calendar_lunar_label = NULL;
    calendar_ganzhi_label = NULL;
    calendar_zodiac_label = NULL;
    calendar_xiu_label = NULL;
    calendar_yi_label = NULL;
    calendar_ji_label = NULL;
    calendar_taishen_label = NULL;
    calendar_chong_label = NULL;
    calendar_pengzu_label = NULL;
    calendar_wuxing_label = NULL;
    
    // 只在确实存在且有效时才删除主屏幕
    if (calendar_screen && lv_obj_is_valid(calendar_screen)) {
        lv_obj_del(calendar_screen);
    }
    calendar_screen = NULL;
}
